﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alerts_Administration_2010.interfaces.views;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Alerts_Administration_2010.classes.views
{
    public class OverviewView : IOverviewView
    {
        private class OverviewItem : IOverviewItem
        {
            public Label UserName = new Label();
            public Label AlertType = new Label();
            public Label AlertName = new Label();
            public Label List = new Label();
            public Label EventType = new Label();
            public LinkButton Edit = new LinkButton();

            string IOverviewItem.AlertName
            {
                set { AlertName.Text = value; }
            }
            string IOverviewItem.UserName
            {
                set { UserName.Text = value; }
            }
            string IOverviewItem.List
            {
                set { List.Text = value; }
            }
            string IOverviewItem.AlertType
            {
                set { AlertType.Text = value; }
            }
            string IOverviewItem.EventType
            {
                set { EventType.Text = value; }
            }
            string IOverviewItem.AlertId
            {
                set
                {
                    Edit.CommandName = "EditAlert";
                    Edit.CommandArgument = value;
                    Edit.Text = "Edit...";
                }
            }
        }

        #region private fields
        private List<IOverviewItem> _items = new List<IOverviewItem>();
        private string _selectedAlertId;
        private Control _container;
        #endregion

        #region public events
        public event EventHandler<EventArgs> EditClicked;
        #endregion

        #region public properties
        public string SelectedAlertId
        {
            get { return _selectedAlertId; }
        }
        public IEnumerable<IOverviewItem> OverviewItems
        {
            get { return _items; }
        }
        #endregion

        #region public methods
        public IOverviewItem CreateOverviewItem(string itemId)
        {
            OverviewItem item = new OverviewItem();
            ((IOverviewItem)item).AlertId = itemId;
            return item;
        }

        public void AddOverviewItem(IOverviewItem overviewItem)
        {
            OverviewItem item = (OverviewItem)overviewItem;
            item.AlertName.ID = "AlertName_" + _items.Count;
            item.AlertType.ID = "AlertType_" + _items.Count;
            item.EventType.ID = "EventType_" + _items.Count;
            item.List.ID = "List_" + _items.Count;
            item.UserName.ID = "UserName_" + _items.Count;
            item.Edit.ID = "Edit_" + _items.Count;
            item.Edit.Click += new EventHandler(Edit_Click);
            _items.Add(item);
        }

        public object Render()
        {
            Panel p = new Panel();
            Table table = new Table();
            AddHeader(table, new string[] { "Name", "User", "List", "AlertType", "EventType", "Actions" });
            foreach (OverviewItem item in _items)
            {
                TableRow row = new TableRow();
                table.Rows.Add(row);
                AddCell(row, item.AlertName);
                AddCell(row, item.UserName);
                AddCell(row, item.List);
                AddCell(row, item.AlertType);
                AddCell(row, item.EventType);
                AddCell(row, item.Edit);
            }
            Button refresh = new Button();
            refresh.ID = "Refresh";
            refresh.Text = "Refresh";

            p.Controls.Add(table);
            p.Controls.Add(refresh);

            _container.Controls.Clear();
            _container.Controls.Add(p);

            return p;
        }
        #endregion

        public OverviewView(Control container)
        {
            _container = container;
        }

        #region event handler
        private void Edit_Click(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton)sender;
            _selectedAlertId = button.CommandArgument;
            if (EditClicked != null) EditClicked(this, EventArgs.Empty);
        }
        #endregion

        #region private methods (Table related)
        private void AddHeaderCell(TableHeaderRow headerRow, string text)
        {
            TableHeaderCell headerCell = new TableHeaderCell();
            headerCell.Text = text;
            headerRow.Cells.Add(headerCell);
        }
        private void AddHeader(Table table, string[] text)
        {
            TableHeaderRow headerRow = new TableHeaderRow();
            table.Rows.Add(headerRow);
            foreach (string s in text)
            {
                AddHeaderCell(headerRow, s);
            }
        }
        private void AddCell(TableRow row, Control control)
        {
            TableCell cell = new TableCell();
            row.Cells.Add(cell);
            cell.Controls.Add(control);
        }
        #endregion


    }
}
